package com.dreamer.repository.mobile;

import com.dreamer.domain.user.Agent;
import com.dreamer.domain.user.AgentVO;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import ps.mx.otter.utils.SearchParameter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangfei on 28/06/2017.
 */
@Repository
public class AgentDao extends BaseDaoImpl<Agent> {

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public List<Agent> findByParentHasUnionId(Integer pid) {
        DetachedCriteria dc = DetachedCriteria.forClass(Agent.class);
        dc.add(Restrictions.eq("parent.id", pid));
        dc.add(Restrictions.isNotNull("wxUnionID"));
        dc.add(Restrictions.ne("wxUnionID", ""));
        //TODO 没有结果不知道这里会返回什么
        return findByCriteria(dc);
    }


    public List<Agent> findAgents(SearchParameter<Agent> parameter) {
//        Example example = Example.create(parameter.getEntity()).enableLike(MatchMode.ANYWHERE);
        DetachedCriteria criteria = DetachedCriteria.forClass(Agent.class);
//        criteria.add(example);

        if(parameter.getEntity().getAgentCode()!=null){
//            criteria.add(Restrictions.eq("parent.id",parameter.getEntity().getParent().getId()));
            Disjunction disjunction = Restrictions.disjunction();
            disjunction.add(Restrictions.like("agentCode","%"+parameter.getEntity().getAgentCode()+"%"));
            disjunction.add(Restrictions.like("mobile","%"+parameter.getEntity().getAgentCode()+"%"));
            disjunction.add(Restrictions.like("weixin","%"+parameter.getEntity().getAgentCode()+"%"));
            disjunction.add(Restrictions.like("realName","%"+parameter.getEntity().getAgentCode()+"%"));
            criteria.add(disjunction);
        }

        if(parameter.getEntity().getParent()!=null){
            criteria.add(Restrictions.eq("parent.id",parameter.getEntity().getParent().getId()));
        }
        //时间
        if (parameter.getStartTime() != null || parameter.getEndTime() != null) {
            criteria.add(Restrictions.between("joinDate", parameter.getStartTimeByDate(), parameter.getEndTimeByDate()));
        }
        criteria.addOrder(Order.desc("joinDate"));
        return searchByPage(parameter,criteria);
    }

    public List<Agent> findChildrens(Integer uid) {
        String hql ="from User user where user.parent.id = :uid";
        Query query  = getCurrentSession().createQuery(hql);
        query.setParameter("uid",uid);
        return query.list();

    }
    public List findAgentCodesAfter(String code){
        String sql = "select distinct agent_code from user where agent_code >= :agentCode order by  agent_code";
        Query query = currentSession().createNativeQuery(sql);
//        query.setParameter("agentCode",agentCode);
        query.setParameter("agentCode",code);
        return  query.list();
    }

    public List findAgentByAddress(String province, String city,String county){
        String queryString = "from User as u WHERE u.regionProfitProvince=:province and u.regionProfitCity=:city and u.regionProfitCounty=:county";
        Query query = getCurrentSession().createQuery(queryString);
        query.setParameter("province",province);
        query.setParameter("city",city);
        query.setParameter("county",county);
        return  query.list();
    }
    public List findAgentByProvinceAndCity(String province, String city){
        String queryString = "from User as u WHERE u.regionProfitProvince=:province and u.regionProfitCity=:city";
        Query query = getCurrentSession().createQuery(queryString);
        query.setParameter("province",province);
        query.setParameter("city",city);
        return  query.list();
    }
    public List findAgentByProvince(String province){
        String queryString = "from User as u WHERE u.regionProfitProvince=:province";
        Query query = getCurrentSession().createQuery(queryString);
        query.setParameter("province",province);
        return  query.list();
    }

    public List<AgentVO> findSubAgentById(Integer id){
        List<AgentVO> lists = new ArrayList<>();
        String sql = "select u.id as id,u.agent_code as agentCode,u.real_name as realName from user u where u.parent = "+id + " and u.agent_code != '' " ;
        Query query = currentSession().createNativeQuery(sql);
        List list = query.list();
        for (int i = 0; i < list.size(); i++) {
            Object[] object = (Object[]) list.get(i);
            AgentVO agentVO = new AgentVO();
            agentVO.setId((Integer) object[0]);
            agentVO.setAgentCode((String) object[1]);
            agentVO.setRealName((String) object[2]);
            lists.add(agentVO);
        }
        return  lists;
    }

}